//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2011 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_ALGORITHM_MD5_HASH_H_
#define _BUOLA_ALGORITHM_MD5_HASH_H_

#include <buola/buola.h>

namespace buola {

///\addtogroup algorithm
///@{

/////////////////////////////////////////////////////////////////////////////////////////////////

/// MD5 hash

/// This class holds an MD5 hash. It is the classed used as the return type for the \ref md5_hash
/// function, and it doesn't perform any calculation, just holds the data
///
/// For more information on MD5, see http://en.wikipedia.org/wiki/MD5
///
/////////////////////////////////////////////////////////////////////////////////////////////////

class CMD5Hash
{
public:
    ///default constructor, does nothing
    CMD5Hash() {}

    ///initialise the hash from a pointer to the data
    explicit CMD5Hash(uint8_t *pDigest)
    {
        for(int i=0;i<16;i++)
            mDigest[i]=pDigest[i];
    }

    ///copy constructor
    CMD5Hash(const CMD5Hash &pO)
    {
        for(int i=0;i<16;i++)
            mDigest[i]=pO.mDigest[i];
    }

    ///returns the data
    uint8_t *Data()             {   return mDigest; }
    ///returns the data (const version)
    const uint8_t *Data() const {   return mDigest; }

    ///returns the data as an hexadecimal string (automatic conversion)
    operator std::string() const
    {
        return hex_string(mDigest,16);
    }
    
    ///returns the data as an hexadecimal wide string (automatic conversion)
    operator std::wstring() const
    {
        return hex_wstring(mDigest,16);
    }

private:
    uint8_t mDigest[16];
};

///\name Hashing
//@{
    
///returns the MD5 hash of \p pString
CMD5Hash md5_hash(const std::string &pString);
///returns the MD5 hash of \p pString
CMD5Hash md5_hash(const std::wstring &pString);
///returns the MD5 hash of \p pLen bytes at \p pData
CMD5Hash md5_hash(const void *pData,size_t pLen);

//@}

///@}

/*namespace buola*/ }

#endif
